#include <iostream>
#include <csignal>
#include <unistd.h>

void signal_handler(int signum) {
    std::cout << "Received signal: " << signum << std::endl;
}
int main() {
    std::cout << "Process ID: " << getpid() << std::endl;
    signal(SIGINT, signal_handler);               // Signal 2: SIGINT
    signal(SIGRTMIN + 6, signal_handler);          // Signal 40: SIGRTMIN + 6

    sigset_t signalSet;
    sigemptyset(&signalSet);
    sigaddset(&signalSet, SIGINT);
    sigaddset(&signalSet, SIGRTMIN + 6);

    if (sigprocmask(SIG_BLOCK, &signalSet, nullptr) != 0) {
        std::cerr << "Failed to block signals." << std::endl;
        return 1;
    }

    std::cout << "Signals 2 (SIGINT) and 40 (SIGRTMIN+6) are now blocked." << std::endl;

    for (int i = 0; i < 20; ++i) {
        std::cout << "Sleeping... (" << i+1 << "/20)" << std::endl;
        sleep(1);
    }

    if (sigprocmask(SIG_UNBLOCK, &signalSet, nullptr) != 0) {
        std::cerr << "Failed to unblock signals." << std::endl;
        return 1;
    }
    std::cout << "Signals 2 (SIGINT) and 40 (SIGRTMIN+6) are now unblocked." << std::endl;

    while (true) {
        pause();
    }

    return 0;
}
